<template>
  <div class="container">
    <div class="row">
      <span>姓名：</span><input type="text" v-model="student.name">
    </div>
    <div class="row">
      <span>年龄：</span><input type="text" v-model="student.age">
    </div>
    <div class="btn_wrap">
      <aside @click="submit" class="btn confirm">ins</aside>
      <aside @click="deleteData" class="btn delete">del</aside>
    </div>

    <section class="list_wrap">
      <table>
        <tr v-for="(item ,index) in stuList" :key="index">
          <td>{{item.name}}</td>
          <td>{{item.age}}</td>
        </tr>
      </table>
    </section>
  </div>
</template>

<script>
export default {
  data() {
    return {
      input1: "d",
      student: {
        name: "章三",
        age: 22
      },
      stuList: []
    };
  },
  created() {
    this.$axios
      .get("http://localhost:3000/getData")
      .then(response => {
        this.stuList = response.data.data;
      })
      .catch(err => {
        console.log(err);
      });
  },
  mounted() {},
  methods: {
    submit() {
      let {name, age} = this.student;
      this.$axios
        .post("http://localhost:3000/insert", {
          name,
          age
        })
        .then(response => {
          this.stuList = response.data.data;
        })
        .catch(err => {
          console.log(err);
        });
    },
    deleteData() {
      this.$axios
        .get("http://localhost:3000/delete", {})
        .then(response => {
          this.stuList = response.data.data;
        })
        .catch(err => {
          console.log(err);
        });
    }
  },
  components: {}
};
</script>
<style scoped lang="less">
.container {
  background: rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(0, 0, 0, 0.1);

  .row {
    margin: 0.2rem auto;
    display: flex;
    justify-content: center;
    align-items: center;

    input {
      border-radius: 0.1rem;
      outline: none;
      box-shadow: none;
      border: none;
      padding: 0.1rem 0 0.1rem 0.2rem;
      color: #666666;
    }
  }

  .btn_wrap {
    margin: 0.5rem auto 0.2rem;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .btn {
    margin: 0 0.1rem;
    user-select: none;
    border-radius: 0.2rem;
    width: 2rem;
    height: 0.8remrem;
    box-shadow: 0 0 3px black;
    text-align: center;
    line-height: 0.8rem;

    &.confirm {
      background: white;
      &:active {
        background: rgba(0, 0, 0, 0.1);
      }
    }

    &.delete {
      background: red;
      color: white;
    }
  }
}
</style>
